import asyncio
from pprint import pprint
from typing import Dict, Any, Optional, List
import datetime
from bilibili_api.user import User, VideoOrder
from bilibili_api import video

USER_INFO = {
    "llm_weekly_report": {
        "uid": 103118875,
        "username":"Akinokoe",
        "keyword": "大模型周报",
        "homepage": "https://space.bilibili.com/103118875",
    },
    "ai_daily_report": {
        "uid": 285286947,
        "username":"橘鸦Juya",
        "keyword": "AI日报",
        "homepage": "https://space.bilibili.com/285286947",
    },
    "weekly_tech_complement": {
        "uid": 67079745,
        "username":"玄离199",
        "keyword": "科技补全",
        "homepage": "https://space.bilibili.com/67079745",
    },
    "weekly_github_hotpoint": {
        "uid": 65564239,
        "username":"IT咖啡馆",
        "keyword": "Github一周热点",
        "homepage": "https://space.bilibili.com/65564239",
    }
}


async def fetch_latest_with_keyword(uid: int, keyword: str) -> Optional[Dict[str, Any]]:
    user = User(uid=uid)
    resp = await user.get_videos(ps=50, pn=1, order=VideoOrder.PUBDATE, keyword=keyword)
    try:
        vlist: List[Dict[str, Any]] = resp["list"]["vlist"]
    except Exception:
        return None
    if not vlist:
        return None
    v = vlist[0]
    
    return {
        "title": v.get("title", ""),
        "bvid": v.get("bvid", ""),
        "description": v.get("description", ""),
        "length": v.get("length", ""),
        "created": datetime.datetime.fromtimestamp(int(v.get("created", 0))).strftime("%Y-%m-%d"),
    }

async def get_video_info(bvid: str) -> Optional[Dict[str, Any]]:
    video_obj = video.Video(bvid="BV1S4snzQEUM")
    pprint(video_obj)
    resp =  await video_obj.get_ai_conclusion(33360774553)
    return resp

def test_fetch_latest_with_keyword():
    res = asyncio.run(fetch_latest_with_keyword(103118875, "大模型周报"))
    print(res)

def test_get_video_info():
    res = asyncio.run(get_video_info("BV1S4snzQEUM"))
    print(res)

if __name__ == "__main__":
    # test_fetch_latest_with_keyword()
    test_get_video_info()
